<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/14
 * Time: 16:03
 */

class swoole_oop_server
{
    /**
     * 保存swoole服务
     * @var \Swoole\Http\Server | \Swoole\WebSocket\Server
     */
    protected $server;
    protected $num;

    public function __construct()
    {
        $this->num = 999;
        $this->server = new Swoole\Http\Server("192.168.182.128", 9501);

        //官方的方式
        //监听request请求事件
        /*$this->server->on('request', function ($request, $response) {
            $response->end("<h1>Hello Swoole-666. #".rand(1000, 9999)."</h1>");
        });*/

        //对象的方式
        $this->server->on('request', [$this, 'onRequest']);
    }

    public function run()
    {
        $this->server->start();
    }

    /**
     * swoole执行的处理闭包
     * @param $request
     * @param $response
     */
    public function onRequest($request, $response)
    {
        $response->end($this->num);
    }

}

$oopServer =  new swoole_oop_server();
$oopServer->run();